package com.power.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.power.mapper.TActivityMapper;
import com.power.model.TActivity;
import com.power.model.TUser;
import com.power.query.ActivityQuery;
import com.power.service.ActivityService;
import com.power.util.LoginInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private TActivityMapper tActivityMapper;

    @Override
    public PageInfo<TActivity> getActivityByPage(Integer currentPage, Integer pageSize, ActivityQuery activityQuery) {
        PageHelper.startPage(currentPage, pageSize);
        List<TActivity> activityList = tActivityMapper.selectActivityByPage(activityQuery);
        PageInfo<TActivity> pageInfo = new PageInfo<>(activityList);
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        BeanUtils.copyProperties(activityQuery, tActivity);
        TUser tUser = LoginInfoUtil.getUserFromToken(activityQuery.getToken());
        tActivity.setCreateBy(tUser.getId());
        tActivity.setCreateTime(new Date());
        return tActivityMapper.insertSelective(tActivity);
    }

    @Override
    public TActivity getActivityDetail(Integer id) {
        return tActivityMapper.selectActivityDetailById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        BeanUtils.copyProperties(activityQuery, tActivity);
        TUser tUser = LoginInfoUtil.getUserFromToken(activityQuery.getToken());
        tActivity.setEditBy(tUser.getId());
        tActivity.setEditTime(new Date());
        return tActivityMapper.updateByPrimaryKeySelective(tActivity);
    }
}
